package demo4接口使用案例;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//自定义类型要想比较大小 必须实现接口 comparable--compareto方法  返回整数 比较大小
    @Override
    public int compareTo(Student o) {
       /* if(this.age>o.age){
            return 1;
        } else if (this.age<o.age) {
            return -1;
        }else {
            return 0;
        }*/
        return this.age-o.age;
    }
}


public class Test {
    //模拟实现 Arrays.sort
   /* public static void bubbleSort(Comparable[] comparable){
        for (int i = 0; i <comparable.length ; i++) {
            for (int j = 0; j < comparable.length-1-i; j++) {
                if(comparable[j].compareTo(comparable[j+1])>0){
                    Comparable tmp=comparable[j];
                    comparable[j]=comparable[j+1];
                    comparable[j+1]=tmp;
                }
            }
        }
    }*/
    public static void main(String[] args) {
        Student student1=new Student("张三",10);
        Student student2=new Student("李四",15);
        //比较两个学生谁大谁小
        //System.out.println(student1>student2);
        //引用类型不可这样比较
        System.out.println(student1.compareTo(student2));

        Student[] students=new Student[3];
        students[0]=new Student("珍妮",23);
        students[1]=new Student("丽萨",22);
        students[2]=new Student("肉丝",22);

        //数组排序
        Arrays.sort(students);
        //bubbleSort(students);
        System.out.println(Arrays.toString(students));//根据年龄排序
    }
}
